﻿using MediatR;
using PublicClass;
using S.RBAC.Domain.Dto;
using S.RBAC.Domain.Model;
using S.RBAC.Infrastructure.Interface;
using S.RBAC.Read.Api.Application.Command.DepartmentCommand;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace S.RBAC.Read.Api.Application.CommandHandlers.DepartmentHandler
{
    public class DepartmentGetAllCommandHandler : IRequestHandler<DepartmentGetAllCommand, PageInfo<DepartmentDto>>
    {
        protected readonly IDepartmentRepository _department;
        protected readonly IDepartTypeRepository _departType;
        protected readonly IStaffRepository _staff;

        public DepartmentGetAllCommandHandler(IDepartmentRepository department, IDepartTypeRepository departType, IStaffRepository staff)
        {
            _department = department;
            _departType = departType;
            _staff = staff;
        }

        public async Task<PageInfo<DepartmentDto>> Handle(DepartmentGetAllCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await _department.GetAllDepartment()
                        join b in await _departType.GetAllDepartType() on a.DepartTypeId equals b.DepartTypeId
                        join c in await _staff.GetAllStaff() on a.StaffId equals c.StaffId
                        where a.IsDel == false && a.DepartName != "宏光养羊"
                        select new DepartmentDto
                        {
                            DepartCode = a.DepartCode,
                            DepartName = a.DepartName,
                            DepartState = a.DepartState,
                            DepartRank = a.DepartRank,
                            DepartLocation = a.DepartLocation,
                            DepartTypeId = b.DepartTypeId,
                            DepartTypeName = b.DepartTypeName,
                            StaffId = c.StaffId,
                            StaffName = c.StaffName,
                            CreateBy = a.CreateBy,
                            CreateDate = a.CreateDate,
                            UpdateBy = a.UpdateBy,
                            UpdateDate = a.UpdateDate,
                        }).ToList();

            if (request.DepartName != null)
            {
                list = list.Where(t => t.DepartName.Contains(request.DepartName)).ToList();
            }
            if (request.DepartTypeId != null)
            {
                list = list.Where(t => t.DepartTypeId == request.DepartTypeId).ToList();
            }
            if (request.DepartState != null)
            {
                list = list.Where(t => t.DepartState == request.DepartState).ToList();
            }

            //总行数
            int totalCount = list.Count;
            //总页数
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            //分页
            list = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            return new PageInfo<DepartmentDto> { totalCount = totalCount, pageCount = pageCount, datas = list };
        }
    }
}
